//
//  SINAStudentAlertQQViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/27.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAStudentAlertQQViewController.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/sdkdef.h>
#import "AppDelegate.h"

@interface SINAStudentAlertQQViewController ()<TencentSessionDelegate,TencentLoginDelegate>
{
    
}

//提交数据 按钮
@property (weak, nonatomic) IBOutlet UIButton *commitAlertPhoneButton;

- (IBAction)onClickQQBindButton:(id)sender;


@end

@implementation SINAStudentAlertQQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    self.navigationItem.title=@"绑定QQ号";
    //设置lable的圆角显示
    self.commitAlertPhoneButton.layer.cornerRadius =5;
    //设置lable的边框 宽度为1
    self.commitAlertPhoneButton.layer.borderWidth=0;
    //设置lable的边框颜色
    self.commitAlertPhoneButton.layer.borderColor=[UIColor redColor].CGColor;
    //设置 显示大小范围为 border内容范围中
    self.commitAlertPhoneButton.clipsToBounds = YES;
    [self.commitAlertPhoneButton setBackgroundColor: [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f]];
}

- (IBAction)onClickQQBindButton:(id)sender {
    
    /**
     * 初始化以及登录授权调用
     
     NSString * appid = __TencentAppid_;
     
     AppDelegate * delegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
     
     delegate.oauth = [[TencentOAuth alloc] initWithAppId:appid
     andDelegate:self];
     
     NSMutableArray *permissions = [[NSMutableArray alloc]initWithObjects:kOPEN_PERMISSION_GET_USER_INFO,kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil];
     
     //登录授权
     [delegate.oauth authorize:permissions inSafari:NO];
     
     */
}

/**
 * \brief TencentLoginDelegate iOS Open SDK 1.3 API回调协议
 *
 * 第三方应用实现登录的回调协议
 */
#pragma mark - TencentLoginDelegate(授权登录回调协议)
/**
 * 登录成功后的回调
 */
- (void)tencentDidLogin
{
    NSLog(@"QQ登录完成");
    AppDelegate * delegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (delegate.oauth.accessToken && 0!=[delegate.oauth.accessToken length]) {
        
        //记录登录用户的OpenID、Token以及过期时间
        NSString *accessToken = [delegate.oauth accessToken];
        NSString *openId = [delegate.oauth openId];
        NSDate *expirationDate = [delegate.oauth expirationDate];
        
        NSString *cachedOpenId = [delegate.oauth getCachedOpenID];
        NSString *cachedToken = [delegate.oauth getCachedToken];
        
        NSLog(@"%@",accessToken);
        NSLog(@"%@",openId);
        NSLog(@"%@",expirationDate);
        
        NSLog(@"%@",cachedOpenId);
        NSLog(@"%@",cachedToken);
        
        //设置
        [delegate.oauth setAccessToken:accessToken];
        [delegate.oauth setOpenId:openId];
        [delegate.oauth setExpirationDate:expirationDate];
        
    }
    else
    {
        //登录不成功，没有获取到accesstoken
    }
}

/**
 * 登录失败后的回调
 * \param cancelled 代表用户是否主动退出登录（非网络错误导致登录失败:）
 */
- (void)tencentDidNotLogin:(BOOL)cancelled
{
    if (cancelled) {
        
        //用户取消登录
    }
    else
    {
        //登录失败
    }
}

/**
 * 登录时网络有问题的回调（网络错误导致登录失败：）
 */
- (void)tencentDidNotNetWork
{
    //检查网络设置
}

/**
 * 登录时权限信息的获得
 */
- (NSArray *)getAuthorizedPermissions:(NSArray *)permissions withExtraParams:(NSDictionary *)extraParams
{
    return nil;
}

/**
 * unionID获得
 */
- (void)didGetUnionID
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
